package com.kingint.web.admin.serve.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.mbg.bas.entity.BasRoom;
import com.kingint.mbg.bas.entity.BasServiceClass;
import com.kingint.mbg.bas.mapper.BasRoomMapper;
import com.kingint.mbg.bas.mapper.BasServiceClassMapper;
import com.kingint.mbg.dto.BackQueryParam;
import com.kingint.mbg.dto.MemberReturn;
import com.kingint.mbg.dto.servcieOrder.OrderQueryParam;
import com.kingint.mbg.dto.servcieOrder.SerServiceOrderParam;
import com.kingint.mbg.ser.entity.SerServiceOrder;
import com.kingint.mbg.ser.entity.SerServiceOrderMiddle;
import com.kingint.mbg.ser.entity.SerWorkOrder;
import com.kingint.mbg.ser.mapper.SerServiceOrderMapper;
import com.kingint.mbg.ser.mapper.SerServiceOrderMiddleMapper;
import com.kingint.mbg.ser.mapper.SerWorkOrderMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.mbg.sys.mapper.UmsMemberMapper;
import com.kingint.web.admin.serve.service.SerServiceOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.portal.serve.service.ServiceOrderService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.WorkType;
import com.kingint.web.util.WorkTypeUtil;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RuntimeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务工单表 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-28
 */
@Service
@Slf4j
public class SerServiceOrderServiceImpl extends ServiceImpl<SerServiceOrderMapper, SerServiceOrder> implements SerServiceOrderService {

    @Resource
    private SerServiceOrderMapper serviceOrderMapper;
    @Resource
    private SerWorkOrderMapper workOrderMapper;
    @Resource
    private SerServiceOrderMiddleMapper serviceOrderMiddleMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private UmsAdminService adminService;
    @Resource
    private UmsMemberMapper memberMapper;
    @Resource
    private BasRoomMapper roomMapper;
    @Resource
    private BasServiceClassMapper basServiceClassMapper;
    @Resource
    public ServiceOrderService serviceOrderService;

    @Override
    public int deleteServiceOrderInfo(Integer id) {
        SerServiceOrder serviceOrder = serviceOrderMapper.selectById(id);
        Integer workId = serviceOrder.getWorkId();
        SerWorkOrder workOrder = new SerWorkOrder();
        workOrder.setId(workId);
        workOrder.setStatus(1);
        int i = workOrderMapper.updateById(workOrder);

        //再修改中间表
        QueryWrapper<SerServiceOrderMiddle> wrapper = new QueryWrapper<>();
        wrapper.eq("service_order_id", id);

        SerServiceOrderMiddle orderMiddle = new SerServiceOrderMiddle();
        orderMiddle.setStatus(1);
        serviceOrderMiddleMapper.update(orderMiddle, wrapper);

        //同时查询是否有当前的流程实例，结束流程实例
        SerWorkOrder serWorkOrder = workOrderMapper.selectById(workId);
        String processInstanceId = serWorkOrder.getProcessInstanceId();

        runtimeService.deleteProcessInstance(processInstanceId, "服务工单总工单已经删除");
        return i;
    }

    @Override
    public IPage<SerServiceOrderParam> getServiceOrderList(BackQueryParam queryParam, Integer pageNum, Integer pageSize) {

        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("该账号未关联酒店id");
            return null;
        }
        queryParam.setHotelId(hotelId);//对应的酒店id

        IPage<SerServiceOrderParam> page = new Page<>(pageNum, pageSize);
        IPage<SerServiceOrderParam> serviceOrderList = serviceOrderMapper.getServiceOrderList(page, queryParam);
        List<SerServiceOrderParam> records = serviceOrderList.getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            //查询待处理人和相关的信息
            records.stream().forEach(s->{
                BackQueryParam param = new BackQueryParam();
                param.setServiceOrderId(s.getId());
                MemberReturn info = memberMapper.getServiceReceiveInfo(param);
                if(!StringUtils.isEmpty(info)){
                    s.setHandleUserId(info.getId());
                    s.setHandleName(info.getUsername());
                }
            });
        }
        return serviceOrderList;
    }

    @Override
    public SerServiceOrderParam getServiceOrderInfo(Integer id) {
        SerServiceOrderParam serviceInfoById = serviceOrderMapper.getServiceInfoById(id);
        Integer roomId = serviceInfoById.getRoomId();
        BasRoom basRoom = roomMapper.selectById(roomId);
        serviceInfoById.setFloorId(basRoom.getFloorId());
        serviceInfoById.setRoomNum(basRoom.getRoomNum());//对应房间信息
        //查询是否有处理人
        QueryWrapper<SerServiceOrderMiddle> wrapper = new QueryWrapper<>();
        wrapper.eq("service_order_id",serviceInfoById.getId())
                .eq("status",0);
        SerServiceOrderMiddle middle = serviceOrderMiddleMapper.selectOne(wrapper);
        if(!StringUtils.isEmpty(middle)){
            Long handleUserId = middle.getHandleUserId();
            UmsMember umsMember = memberMapper.selectById(handleUserId);
            serviceInfoById.setHandleName(umsMember.getUsername());//处理人
            serviceInfoById.setDepartmentId(umsMember.getDepartmentId());
        }
        //处理json
        String content = serviceInfoById.getServiceClassContent();
        if(!StringUtils.isEmpty(content)){
            List<BasServiceClass> list = JSONObject.parseArray(content, BasServiceClass.class);
            serviceInfoById.setServiceClassList(list);
        }
        return serviceInfoById;
    }

    @Override
    public Integer insertServiceInfo(SerServiceOrder serServiceOrder) {
        List<Integer> classId = serServiceOrder.getServiceClassId();
        if(!CollectionUtils.isEmpty(classId)){
            QueryWrapper<BasServiceClass> wrapper = new QueryWrapper<>();
            wrapper.in("id",classId);
            List<BasServiceClass> list = basServiceClassMapper.selectList(wrapper);
            serServiceOrder.setServiceClassList(list);
            int i = serviceOrderService.insertServiceOrderInfo(serServiceOrder);
            return i;
        }
        return 0;
    }
}
